package com.nq.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.nq.common.LoginType;
import com.nq.common.ServerResponse;
import com.nq.dao.UserMapper;
import com.nq.pojo.SiteSmsLog;
import com.nq.service.ISiteSmsLogService;
import com.nq.service.MailService;
import com.nq.utils.DateTimeUtil;
import com.nq.utils.email.SendHTMLMail;
import com.nq.utils.redis.RedisShardedPoolUtils;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping({"/api/mail/"})
public class MailApiController {

    private static final Logger log = LoggerFactory.getLogger(MailApiController.class);

    @Autowired
    private MailService mailService;

    @Autowired
    ISiteSmsLogService iSiteSmsLogService;
    @Resource
    private SendHTMLMail sendHTMLMail;

    @Resource
    UserMapper userMapper;

    /**
     * 注册，发送邮箱
     * @return
     */
    @RequestMapping({"sendRegMail.do"})
    @ResponseBody
    public ServerResponse<String> sendRegMail(String email) throws MessagingException {
        if (StrUtil.isBlank(email)) {
            return ServerResponse.createByErrorMsg("邮箱不能为空");
        }
        // 判断邮箱是否存在，如果存在则给出提示
        val byEmail = userMapper.findByPhoneOrEmail(email);
        if (null != byEmail) {
            return ServerResponse.createByErrorMsg("电子邮件已存在，请直接登录.");
        }
        String key = "V_MAIL_CODE_" + email;
        String value = RandomUtil.randomNumbers(6);
        value = "9999";
//        boolean bl = sendHTMLMail.sendRegEmail(email, value);
//        if (!bl) {
//            return ServerResponse.createByErrorMsg("发送验证码失败.");
//        }
        RedisShardedPoolUtils.setEx(key, value, 3000);
        return ServerResponse.createBySuccess("发送成功");
    }


    /**
     * 忘记密码，发送邮箱
     * @return
     */
    @RequestMapping({"sendForgetMail.do"})
    @ResponseBody
    public ServerResponse<String> sendForgetMail(String email) throws MessagingException {
        if (StrUtil.isBlank(email)) {
            return ServerResponse.createByErrorMsg("请输入邮箱");
        }
        // 判断邮箱是否存在，如果不存在则给出提示
        val byEmail = userMapper.findByPhoneOrEmail(email);
        if (null == byEmail) {
            return ServerResponse.createByErrorMsg("发送失败，邮箱不存在.");
        }

        String key = "V_FORGET_MAIL_CODE_" + email;
        String value = RandomUtil.randomNumbers(6);

        value = "9999";

//        boolean bl = sendHTMLMail.sendForgetEmail(email, value);
//        if (!bl) {
//            return ServerResponse.createByErrorMsg("Failed to send verification code.");
//        }
        RedisShardedPoolUtils.setEx(key, value, 300);
        return ServerResponse.createBySuccess("发送成功");
    }

    /**
     * 交易行为需要发送邮件提醒
     * @return
     */
    @RequestMapping({"tradingBehaviorSendMail.do"})
    @ResponseBody
    public ServerResponse<String> tradingBehaviorSendMail(String email) throws MessagingException {



        if (StrUtil.isBlank(email)) {
            return ServerResponse.createByErrorMsg("邮箱不能为空");
        }
        String key = "V_FORGET_MAIL_CODE_" + email;
        String value = RandomUtil.randomNumbers(6);
        String content = String.format("【CSLME】Your verification code is %s, if you are not operating it, please ignore it", value);
        mailService.sendMail(email, "Forgot password", content);

        // Debug 模式默认9999
        RedisShardedPoolUtils.set(key, value);
        SiteSmsLog siteSmsLog = new SiteSmsLog();
        siteSmsLog.setSmsPhone(email);
        siteSmsLog.setSmsTitle("交易行为需要发送邮件提醒");
        siteSmsLog.setSmsCnt(value);
        siteSmsLog.setSmsStatus(Integer.valueOf(0));
        siteSmsLog.setSmsTemplate("字段无用");
        siteSmsLog.setType(LoginType.EMAIL.getCode());
        siteSmsLog.setAddTime(DateTimeUtil.getCurrentDate());
        iSiteSmsLogService.addData(siteSmsLog);
        return ServerResponse.createBySuccess("发送成功");
    }
}
